﻿Imports extranet.BE
Imports extranet.BC
Imports MetodosComuns
Imports System.Collections.Generic

Partial Class ranking_PubRnkVisualizarRanking
  Inherits System.Web.UI.Page
  Private Const ID_RANKING_GERAL As String = "1"

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    '==================       PROCEDIMENTO  PADRÃO  SEGURANÇA     =============================
    Dim objBESeguranca As BESeguranca
    Dim objBEUsuario As BEUsuario = Session("objBEUsuario")
    If IsNothing(objBEUsuario) Then Response.Redirect(TELA_LOGOUT)
    Session("strTela") = MetodosComuns.GetCurrentPageName()
    If Not IsPostBack Then          ' SEGURANÇA
      objBESeguranca = BCSeguranca.SelecionaPermissoes(objBEUsuario.GrupoAcesso, Session("strTela"))
      If Not objBESeguranca.Autorizado Then Response.Redirect(TELA_LOGOUT)
      Session("objBESeguranca") = objBESeguranca
    Else
      objBESeguranca = Session("objBESeguranca")
    End If

    '==================       ALTERAR SE FOR NECESSARIO          ===============================
    If Not IsPostBack Then
      mostraMensagens()
    End If
  End Sub

  Protected Sub grvRankings_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grvRankings.SelectedIndexChanged
    BuscarRanking(grvRankings.DataKeys(grvRankings.SelectedIndex).Value.ToString())
  End Sub

  Private Sub BuscarRanking(ByVal strIdRanking As String)
    Dim objBERanking As BERanking = BCRanking.SelecionarRanking(strIdRanking)

    accRanking.DataSource = BCRankeado.LerRankeados(objBERanking.Planilha, False)
    accRanking.DataBind()

    For i As Integer = 0 To accRanking.Panes.Count - 1
      Dim hfCPF As HiddenField = accRanking.Panes(i).FindControl("hfCPF")

      Select Case i
        Case 0
          Dim imgOuro As Image = accRanking.Panes(i).FindControl("imgOuro")
          imgOuro.Visible = True
        Case 1
          Dim imgPrata As Image = accRanking.Panes(i).FindControl("imgPrata")
          imgPrata.Visible = True
        Case 2
          Dim imgBronze As Image = accRanking.Panes(i).FindControl("imgBronze")
          imgBronze.Visible = True
        Case Else
          Dim lblPos As Label = accRanking.Panes(i).FindControl("lblPos")
          lblPos.Visible = True
          lblPos.Text = (i + 1).ToString()
      End Select

      If hfCPF.Value.ToString() <> "" Then
        Dim panMensagem As Panel = accRanking.Panes(i).FindControl("panMensagem")
        Dim grvMensagens As GridView = accRanking.Panes(i).FindControl("grvMensagens")
        Dim img As Image = accRanking.Panes(i).FindControl("img")
        Dim lnkRankeado As LinkButton = accRanking.Panes(i).FindControl("lnkRankeado")
        Dim hfFranquia As HiddenField = accRanking.Panes(i).FindControl("hfFranquia")
        Dim hfNome As HiddenField = accRanking.Panes(i).FindControl("hfNome")

        Dim objBERankeado As New BERankeado
        Dim objBERankeadoDoBanco As New BERankeado

        objBERankeado.IdRanking = ID_RANKING_GERAL
        objBERankeado.CPF = hfCPF.Value.ToString()
        objBERankeado.Franquia = hfFranquia.Value.ToString()
        objBERankeado.Usuario = New BEUsuario(hfNome.Value.ToString())

        panMensagem.Visible = True

        objBERankeadoDoBanco = BCRankeado.SelecionarRankeado(objBERankeado, 10)

        If objBERankeadoDoBanco.Usuario.Usuario = "" Then
          'If objBERankeado.Usuario.Avatar <> "" Then
          img.Visible = False
          'End If
          lnkRankeado.Visible = False
        Else
          img.ImageUrl &= objBERankeadoDoBanco.Usuario.Avatar
        End If

        grvMensagens.DataSource = objBERankeadoDoBanco.Mensagens
        grvMensagens.DataBind()

      End If
    Next
  End Sub

  Protected Sub EnviarMensagem(ByVal sender As Object, ByVal e As System.EventArgs)
    If accRanking.SelectedIndex <> -1 Then
      Dim txtMensagem As TextBox = accRanking.Panes(accRanking.SelectedIndex).FindControl("txtMensagem")
      Dim hfCPF As HiddenField = accRanking.Panes(accRanking.SelectedIndex).FindControl("hfCPF")
      Dim hfFranquia As HiddenField = accRanking.Panes(accRanking.SelectedIndex).FindControl("hfFranquia")
      Dim hfNome As HiddenField = accRanking.Panes(accRanking.SelectedIndex).FindControl("hfNome")

      Dim objBERankeado As New BERankeado

      objBERankeado.IdRanking = grvRankings.DataKeys(grvRankings.SelectedIndex).Value.ToString()
      objBERankeado.CPF = hfCPF.Value.ToString()
      objBERankeado.Franquia = hfCPF.Value.ToString()
      objBERankeado.Usuario = New BEUsuario(hfCPF.Value.ToString())

      If Session("objBEUsuario") Is Nothing Then
        Response.Redirect("~/Default.aspx")
      Else
        Dim objBEUsuario As BEUsuario = Session("objBEUsuario")

        Try
          BCRankeado.EnviarMensagem(txtMensagem.Text, objBERankeado, objBEUsuario)
          txtMensagem.Text = ""
          System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
          System.Web.HttpContext.Current.Response.Write("alert(""Mensagem Enviada para aprovação da administração. Assim que aprovada, a mensagem será visível por todos os usuários."")" & vbCrLf)
          System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
        Catch ex As ApplicationException
          System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=""JavaScript"">" & vbCrLf)
          System.Web.HttpContext.Current.Response.Write("alert(""Erro no envio da mensagem!"")" & vbCrLf)
          System.Web.HttpContext.Current.Response.Write("</SCRIPT>")
        End Try

      End If
    End If
  End Sub

  Protected Sub VisualizarPerfil_Remetente(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)
    Session("UsuarioParaConsulta") = DirectCast(accRanking.Panes(accRanking.SelectedIndex).FindControl("grvMensagens"), GridView).DataKeys(e.NewEditIndex)("Remetente")
    Response.Redirect("~/Perfil.aspx")
  End Sub

  Protected Sub VisualizarPerfil_Rankeado(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim hfCPF As HiddenField = accRanking.Panes(accRanking.SelectedIndex).FindControl("hfCPF")
    Session("UsuarioParaConsulta") = BCUsuario.SelecionarPeloCPF(hfCPF.Value).Usuario
    Response.Redirect("~/Perfil.aspx")
  End Sub

  Protected Sub grvMensagens_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim objBEUsuario As BEUsuario = Session("objBEUsuario")
    For Each objRow As GridViewRow In DirectCast(sender, GridView).Rows()
      If Not (DirectCast(objRow.FindControl("hdn_Remetente"), HiddenField).Value = objBEUsuario.Usuario OrElse _
         objBEUsuario.GrupoAcesso.Nome.ToUpper() = "ADMIN") Then
        With DirectCast(objRow.FindControl("btnExcluir"), ImageButton)
          .CommandName = ""
          .ImageUrl = "~/Imagens/cleardot.gif"
        End With
      End If
    Next
  End Sub

  Protected Sub grvMensagens_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
    Try
      Dim objGridView As GridView = sender
      Dim objMensagem As New BEMensagem
      objMensagem.Id = Integer.Parse(objGridView.DataKeys(e.RowIndex)("Id"))
      BCMensagem.Deletar(objMensagem)
      grvMensagens_RowDeleted(sender, Nothing)
    Catch ex As Exception
      Throw ex
    End Try
  End Sub

  Protected Sub grvMensagens_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs)
    If e IsNot Nothing Then e.ExceptionHandled = True
    mostraMensagens()
  End Sub

  Protected Sub mostraMensagens()
    grvRankings.DataSource = BCRanking.ListarRanking(True)
    grvRankings.DataBind()
    BuscarRanking(ID_RANKING_GERAL)
    If grvRankings.Rows.Count > 0 Then grvRankings.SelectedIndex = ID_RANKING_GERAL - 1
  End Sub

End Class
